package com.reach.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.reach.redis.manager.SpringContextUtil;

@Configuration
public class Config {

    @Bean(name = "redis")
    public RedisTemplate<String,Object> redisTemplateInit() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
    	RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) SpringContextUtil.getBean("redisTemplate");
        template.setConnectionFactory(redisTemplate.getConnectionFactory());
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return redisTemplate;
    }
}

